<template>
	<view class="form-container">
		<!-- form演示 -->
		<view class="form-group">
			<form class="form-main">
				<view class="form-group">
					<h3>托盘码</h3>
					<view>
						<input id="user-insert-line" :focus="focus1" type="number" name="input" @input="INPUT"
							v-model="model.traycode" placeholder="托盘码" />
					</view>
				</view>
				<view class="form-group">
					<h3>物料数量</h3>
					<view>
						<input id="user-insert-line" :focus="focus2" type="number" name="input"
							v-model="model.MaterialNumber" placeholder="数量" />
					</view>
				</view>
				<view class="form-group">
					<uni-section title="选择砂芯设备" type="line"></uni-section>
					<view class="example-body">
						<combox-search label="砂芯设备" labelWidth="100px" emptyTips="无匹配选项" :isJSON="true"
							:keyName="keyNameCode" :candidates="candidatesDev" placeholder="选择砂芯设备"
							@getValue="getValue($event)">
						</combox-search>
					</view>
				</view>
				<view class="form-group">
					<h5>补空车（勾选自动补空车）</h5>
					<view class="radio-group">
						<checkbox-group @change="selfChangde" name="isSupply">
							<label>
								<checkbox :checked="selfChecked" style="transform:scale(0.8)" />
								<text>补空车</text>
							</label>
						</checkbox-group>
					</view>
				</view>
				<!-- 新增button按钮触发submit事件 -->
				<view class="form-group"><button class="btnsubmit" :class='{active:isAddShow==true}'
						:disabled="isAddShow" @click="deterMine" form-type="submit">呼叫小车</button>
				</view>
				<!-- <view class="form-group"><button @click="postWCS" form-type="submit">上报WCS</button>
				</view> -->
				<view class="form-group"><button @click="postMES" form-type="submit">上报MES</button>
				</view>
				<view class="form-group"><button type="default" @click="clear" form-type="reset">重置</button></view>
			</form>
		</view>
	</view>
</template>

<script>
	import comboxSearch from "@/components/cuihai-combox/cuihai-combox.vue"
	import uniSection from "@/components/uni-section.vue"
	export default {
		components: {
			comboxSearch,
			uniSection
		},
		data() {
			return {
				candidates: null,
				productionInfo: null,
				model: {
					traycode: '',
					MaterialNumber: ''
				},
				focus1: true,
				focus2: false,
				candidatesDev: [{
					devCode: '128925',
				}, {
					devCode: '128926',
				}, {
					devCode: '128927',
				}, {
					devCode: '128928',
				}],
				keyNameCode: 'devCode',
				keyName: 'materialType',
				isAddShow: false,
				selfChecked: false
			}
		},
		onLoad() {
			this.screenHeight = uni.getSystemInfoSync().windowHeight;
		},
		methods: {
			getValue(e, type) {
				this.dev = this.candidatesDev[e].devCode;
				console.log(e);
			},
			selfChangde(e) {
				this.selfChecked = !this.selfChecked;
				if (this.selfChecked == true) {
					this.isSupply = 1
					console.log('----' + this.isSupply)
					console.log('=====' + this.selfChecked)
				} else if (this.selfChecked == false) {
					this.isSupply = 0
				}
			},
			deterMine() {
				const that = this
				uni.showModal({
					title: '提示',
					content: that.dev + '设备确定呼叫小车吗?',
					success: function(res) {
						if (res.confirm) {
							if (that.dev == null) {
								uni.showToast({
									title: '请选择制芯机',
									icon: "none",
									duration: 3000,
									position: "center",
								});
								return
							} else {
								uni.request({
									/* 网页 调试接口 */
									//url: '/api/PDA/CallMaterial',
									// url: that.$global.url1+':5000/PDA/CallMaterial',
									url: that.$global.url1 + ':5000/PDA/CallMaterial',
									method: 'POST',
									sslVerify: false,
									data: {
										"CallStation": that.dev,
										"isSupply": that.isSupply,
									},
									header: {
										"Content-Type": "application/json; charset=UTF-8"
									},
									success: (res) => {
										if (res.statusCode == 200) {
											if (res.data.code == 0) {
												that.isAddShow = true
												uni.showToast({
													icon: 'success',
													title: '呼叫成功!',
													mask: true,
												});
												setTimeout(function() {
													that.isAddShow = false
													console.log('1231313' + this.dev)
												}, 1000 * 60);
											} else if (res.data.code == 2) {
												uni.showToast({
													icon: 'success',
													title: res.data.message,
													mask: true,
												});
											}
										} else {
											uni.showToast({
												icon: 'error',
												mask: true,
												title: "呼叫失败!",
											})
										}
									},
									fail: (res) => {
										uni.showToast({
											icon: 'error',
											mask: true,
											title: "接口请求失败!",
										})
										console.log("error===" + res.errMsg)
									}
								});
							}
						} else if (res.cancel) {
							return
						}
					}
				})
			},
			postMES() {
				const that = this
				uni.showModal({
					title: '提示',
					content: '条码' + that.model.traycode + '确定上报MES吗',
					success: function(res) {
						if (res.confirm) {
							uni.request({
								url: that.$global.url2 + ':8090/apis/Acc/ProductProcess/DoSub',
								//url: 'http://10.3.15.130:8090/apis/Acc/ProductProcess/DoSub',
								method: 'POST',
								sslVerify: false,
								data: {
									"MachineId": that.dev,
									"MachineType": 501,
									"ActionTypeId": 2,
									"data": {
										"Quantity": that.model.MaterialNumber,
										"TrayCode": that.model.traycode,
									}
								},
								header: {
									"Content-Type": "application/json; charset=UTF-8"
								},
								success: (res) => {
									if (res.statusCode == 200 && res.data.ret == 1) {
										uni.showToast({
											icon: 'success',
											title: '上传MES成功!',
											mask: true,
										});
										uni.request({
											url: that.$global.url1 +
												':5000/PDA/BarCodeInfos',
											method: 'POST',
											sslVerify: false,
											data: {
												"Station": that.dev,
												"barCode": that.model.traycode,
											},
											header: {
												"Content-Type": "application/json; charset=UTF-8"
											},
											success: (res) => {
												if (res.statusCode == 200 && res.data
													.code == 0) {
													uni.showToast({
														icon: 'success',
														title: '上传WCS成功!',
														mask: true,
													});
												}
											},
											fail: (res) => {
												uni.showToast({
													icon: 'none',
													mask: true,
													title: "插入表失败接口请求失败!",
												})
												console.log("none===" + res.errMsg)
											}
										})
									} else {
										uni.showToast({
											icon: 'none',
											mask: true,
											title: res.data.data,
										});
										console.log('上传失败' + res)
									}
								},
								fail: (res) => {
									uni.showToast({
										icon: 'none',
										mask: true,
										title: "接口请求失败!",
									});
								}
							});

						} else if (res.cancel) {
							return
						}
					}
				})
			},
			// postWCS(){
			// 	const that=this
			// 	uni.showModal({
			// 		title:'提示',
			// 		content:'条码'+that.model.traycode+'确定上报MES吗',
			// 		success:function(res){
			// 			if(res.confirm){
			// 				if (that.dev==null||that.model.traycode=="") {
			// 					uni.showToast({
			// 						title: '砂芯设备或者托盘码为空',
			// 						icon:"none",
			// 						duration:2000,
			// 						position:"top"
			// 					});
			// 					return
			// 				}
			// 				else{
			// 					uni.request({
			// 						/* 网页 调试接口 */
			// 						url: that.$global.url1+':5000/PDA/BarCodeInfos',
			// 						//url: 'http://10.5.51.252:5000/PDA/BarCodeInfos',
			// 						method: 'POST',
			// 						sslVerify: false,
			// 						data: {
			// 							"Station": that.dev,
			// 							"barCode": that.model.traycode,
			// 						},
			// 						header: {
			// 							"Content-Type": "application/json; charset=UTF-8"
			// 						},
			// 						success: (res) => {
			// 							if (res.statusCode == 200&&res.data.code == 0) {
			// 								uni.showToast({
			// 									icon: 'success',
			// 									title: '上传MES成功!',
			// 									mask: true,
			// 								});
			// 							}
			// 						},
			// 						fail: (res) => {
			// 							uni.showToast({
			// 								icon: 'none',
			// 								mask: true,
			// 								title: "插入表失败接口请求失败!",
			// 							})
			// 							console.log("none===" + res.errMsg)
			// 						}
			// 					})
			// 				}
			// 			}else if(res.cancel){
			// 				return
			// 			}
			// 		}
			// 	})
			// },
			clear() {
				this.productionInfo = null
			},
			INPUT(e) {
				console.log(e.detail.value)
				if (e.detail.value.length == 17) {
					this.focus1 = false,
						this.focus2 = true
				}
			},
		}
	}
</script>
<style lang="scss">
	page {
		background-color: #EEF0F4;
		font-size: 18px;
	}

	u-button {
		float: left;
		display: inline;
	}

	.form {
		margin-top: 35rpx;
		width: 100%;
		padding: 0 20rpx;
	}

	.form-group {

		padding: 20rpx 30rpx 0rpx 30rpx;
	}

	.btnsubmit {
		margin-top: 50px;
	}

	#user-insert-line::before {
		position: absolute;
		right: 25px;
		bottom: 0;
		top: 5rem;
		left: 40px;
		height: 1px;
		content: '';
		-webkit-transform: scaleY(.5);
		transform: scaleY(.5);
		background-color: #c8c7cc;
	}

	#name::before {
		position: absolute;
		right: 25px;
		bottom: 0;
		top: 8.8rem;
		left: 40px;
		height: 1px;
		content: '';
		-webkit-transform: scaleY(.5);
		transform: scaleY(.5);
		background-color: #c8c7cc;
	}
</style>
